package co.edu.unal.franquicias.mastercard.resources;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.UriInfo;

import co.edu.unal.franquicias.visa.activities.SimulateOrderActivity;
import co.edu.unal.franquicias.visa.activities.ValidateCreditCardTransactionActivity;
import co.edu.unal.franquicias.visa.domain.CreditCardTransaction;
import co.edu.unal.franquicias.visa.exceptions.NoSuchTransactionException;
import co.edu.unal.franquicias.visa.representations.CreditCardTransactionRepresentation;
import co.edu.unal.franquicias.visa.representations.Representation;
import co.edu.unal.franquicias.visa.representations.VisaUri;

@Path("/creditCard/")
public class CreditCardTransactionResource {

	public CreditCardTransactionResource() {
	}

	private @Context
	UriInfo uriInfo;

	@POST
	@Consumes(Representation.VISA_MEDIA_TYPE)
	@Produces(Representation.VISA_MEDIA_TYPE)
	public Response validateTransaction(
			CreditCardTransaction creditCardTransaction) {
		try {
			CreditCardTransactionRepresentation responseRepresentation = new ValidateCreditCardTransactionActivity()
					.validate(creditCardTransaction,
							new VisaUri(uriInfo.getRequestUri()));
			return Response.ok().entity(responseRepresentation).build();
		} catch (NoSuchTransactionException nsoe) {
			nsoe.printStackTrace();
			return Response.status(Status.NOT_FOUND).build();
		} catch (Exception ex) {
			ex.printStackTrace();
			return Response.serverError().build();
		}
	}

	@GET
	@Produces(Representation.VISA_MEDIA_TYPE)
	public Response check() {
		 CreditCardTransactionRepresentation responseRepresentation = new
		 SimulateOrderActivity().simulate(new
		 VisaUri(uriInfo.getRequestUri()));
		
		 return Response.ok().entity(responseRepresentation).build();
	}
}
